import { pay } from '@/api/goodIndent'
import { unifiedPayment, payOffline, chaoPay } from '@/api/pay'
import VueCountdown from '@chenfengyuan/vue-countdown';
import VueQrcode from '@chenfengyuan/vue-qrcode';
import { detail as getUserDetail, userList } from '@/api/user'
export default {
  components: { VueCountdown, VueQrcode },
  layout: 'cart',
  middleware: 'auth',
  head() {
    return {
      title: this.$t('money.title') + '-' + process.env.APP_NAME,
    }
  },
  asyncData ({ app }) {
    // const script = document.createElement('script');
    // script.src = 'https://developers.oceanpayment.com/assets/img/payment/sha256.js';
    // script.async = true;
    // app.body.appendChild(script);
  },
  data() {
    return {
      loading: true,
      detail: false,
      centerDialogVisible: false,
      buttonLoading: false,
      secureCode: '8lf084dLr40Bf602P4T4048f86Dnnf4b48B20808r6j060J80ZPfvZjPrV2Pz4pX',
      signValue: "",
      qrcode: '',
      timer: null,
      checked: false,
      user: "",
      form: {
        account: '',
        terminal: '',
        order_number: '',
        order_currency: '',
        order_amount: 0,
        methods: 'KOR_Credit Card',
        billing_lastName: '',
        billing_firstName: '',
        billing_email: '',
        billing_phone: '',
        key: '/'
      },
      clicked: 0,
      type: 0,
      list: {},
      types: ['Naverpay', 'SamsungPay', 'KOR_Credit Card', 'rbank', 'wechat', 'payco'],
      rules: {
        cardNo: [
          { required: true, message: '请输入', trigger: 'blur' },
        ],
        date1: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        Cvc: [
          { required: true, message: '请输入Cvc', trigger: 'blur' },
        ]
      }
    }
  },
  mounted() {
    // const script = document.createElement('script');
    // script.src = 'https://secure.oceanpayment.com/pages/js/oceanpayment.js'
    // script.async = true;

    // script.onload = () => {
    //   // 脚本加载完成后的操作
    //   console.log('External script loaded and executed.');
    // };

    // document.body.appendChild(script);
    // Oceanpayment.init(true, '', '');
    $nuxt.$store.commit('setCartTitle', this.$t('money.title'));
    this.getList()
    this.getInfo()
    // this.signValue = sha256_digest(this.form.account + this.form.terminal + this.form.order_number
    //   + this.form.order_currency + this.form.order_amount + this.form.billing_firstName
    //   + this.form.billing_lastName + this.form.billing_email + this.secureCode);
    // console.log(this.signValue)
    // this.payment()
  },
  methods: {
    getInfo () {
      getUserDetail().then(res => {
        this.user = res
      })
    },
    getList() {
      pay($nuxt.$route.query.id).then(response => {
        console.log("🚀 ~  response: ", response)
        if (response.state !== 1) {  // 订单发生改变时，直接跳转到结果页
          if (response.state === 4) {
            // $nuxt.$router.replace('/user/indent/list')
            $nuxt.$router.replace({ path: '/user/indent/detail', query: { id: $nuxt.$route.query.id } })
          } else {
            $nuxt.$router.replace('/user/indent/list')
          }

        }
        this.loading = false;
        this.list = response
      }).catch(error => {
        this.$message({
          message: this.$t('common.arguments'),
          type: 'error'
        });

      })
    },
    handleCheck(val) {
      console.log("🚀 ~  val: ", val)
      this.type = val
      this.clicked = 0
    },
    handleClick(val) {
      console.log("🚀 ~  val: ", val)
      this.type = val
      this.clicked = val
      this.checked = false
    },
    // 显示详情
    showDetail() {
      this.detail = !this.detail
    },
    // 支付
    payment() {
      // $('#checkout_creditcard').submit();
      localStorage.removeItem('formData')

      if (this.type == 0) {
        this.$message.warning('결제방법')
        return false
      }
      this.buttonLoading = true
      if (this.type == 6) {
        payOffline({
          id: $nuxt.$route.query.id,
        }).then(response => {
          this.$message.success('주문완료,결제확인중')
          setTimeout(() => {
            $nuxt.$router.replace('/user/indent/list')
          }, 1000)
        }).catch(error => {
          this.buttonLoading = false
        })
      } else {
        unifiedPayment({
          // email:this.user.email:"jbeier@qq.com"
          id: $nuxt.$route.query.id,
          platform: 'ocean',
          trade_type: this.types[this.type - 1],
          type: 'goodsIndent',
          order_detail: `https://www.drk88.com/money/success?id=${$nuxt.$route.query.id}`
        }).then(response => {
          this.form = { ...this.form, ...response.postdata, billing_address: this.list.good_location.location + '/' + this.list.good_location.house }
          localStorage.setItem('formData', JSON.stringify(this.form))
          this.centerDialogVisible = true
        }).catch(error => {
          this.buttonLoading = false
        })
      }
      // }
      // if (type === 1) { // 余额支付
      //   balancePay({ id: $nuxt.$route.query.id }).then(response => {
      //     this.buttonLoading = false
      //     $nuxt.$router.replace('/money/success')
      //   })
      // } else {
      //   unifiedPayment({
      //     id: $nuxt.$route.query.id,
      //     platform: type,
      //     trade_type: 'NATIVE',
      //     type: 'goodsIndent'
      //   }).then(response => {
      //     this.centerDialogVisible = true
      //     this.qrcode = response.code_url
      //     this.buttonLoading = false
      //     if (this.timer) {
      //       clearInterval(this.timer)
      //       this.timer = setInterval(() => {
      //         this.getList()
      //       }, 5000);
      //     } else {
      //       this.timer = setInterval(() => {
      //         this.getList()
      //       }, 5000);
      //     }
      //   }).catch(error => {
      //     this.$message({
      //       message: this.$t('money.error'),
      //       type: 'error'
      //     });
      //     this.buttonLoading = false
      //   })
      // }
    },
    refresh(postData) {
      window.location.reload()
    },
    close () {
      window.location.reload()
    }
  },
  destroyed() {
    clearInterval(this.timer)
  }
}
